var database = require('../../data/data.js');
Page({

  data: {

  },
  onLoad: function (options) {
    this.setData({
      stadiumTitle: options.stadiumTitle
    })
    var that = this;
    if (database.unionid == "") {
      wx.login({
        success(res) {
          var code = res.code;
          wx.request({
            url: database.url + "/api/V2/user/access_token",
            data: {
              clientId: "QqKTO5BTPN6REI26QAgq",
              clientSecret: "F2MjbLO88FMiwr3pTOXylnjURAOzvqLJXSFMeJUg"
            },
            success(res) {
              database.accessToken = res.data.data.accessToken;
              wx.request({
                url: database.url + '/api/V2/integral/integral_openid',
                method: "POST",
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                data: {
                  code: code,
                  accessToken: database.accessToken,
                  weChatId: "D0C62A3A0C1F4BBD9BA7A35351C685C7",
                  regionName: "other"
                },
                success(res) {
                  var sessoionKey = res.data.data.session_key;
                  wx.getUserInfo({
                    data: {
                      withCredentials: true,
                      lang: "zh_CN"
                    },
                    success(res) {
                      wx.request({
                        url: database.url + '/api/V2/user/we_chat_user',
                        method: "POST",
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        data: {
                          accessToken: database.accessToken,
                          weChatId: "D0C62A3A0C1F4BBD9BA7A35351C685C7",
                          encryptedData: res.encryptedData,
                          iv: res.iv,
                          sessionKey: sessoionKey,
                          weChatName: "场馆小程序"
                        },
                        success(res) {
                          database.unionid = res.data.data.unionId;
                          wx.request({
                            url: database.url + '/api/V2/user/verify_wechat_small',
                            method: "POST",
                            header: {
                              'content-type': 'application/x-www-form-urlencoded'
                            },
                            data: {
                              accessToken: database.accessToken,
                              unionid: database.unionid
                            },
                            success(res) {
                              if (res.data.data.verify == "yes") {
                                database.userId = res.data.data.id
                                database.login = true;
                              } else {
                                database.login = false;
                              }
                            }
                          })
                        }
                      })
                    }
                  })
                }
              })
            }
          })
        }
      })
    }
    this.setData({
      url: database.url + "/front/wechat/stadium/advertise_details?advertiseId=" + options.advertiseId + "&unionId=" + database.unionid,
      advertiseId: options.advertiseId
    })
  },
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.stadiumTitle,
      path: '/pages/swiperDetail/swiperDetail?advertiseId=' + that.data.advertiseId + '&stadiumTitle=' + that.data.stadiumTitle,
      success: function (res) {
        wx.showToast({
          title: '转发成功',
          icon: 'success',
          duration: 1500
        })
      }
    }
  }
})